package com.group02.community.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group02.community.entity.vo.VisitorVO;
import com.group02.community.mapper.VisitorMapper;
import com.group02.community.service.IVisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class VisitorServiceImpl extends ServiceImpl<VisitorMapper,VisitorVO > implements IVisitorService {
    @Autowired
    private VisitorMapper visitorsMapper;

    @Override
    public  IPage<VisitorVO> getVisitorPage(Long current, Long size, VisitorVO visitorVO) {
        System.out.println(visitorsMapper.selectVisitorPage(new Page<VisitorVO>(current, size), visitorVO).getTotal());
        return visitorsMapper.selectVisitorPage(new Page<VisitorVO>(current,size),visitorVO);
    }

    @Override
    public void addVisitor(VisitorVO visitorVO) {
        visitorsMapper.addVisitor(visitorVO);
    }
    /**
     * Excel文件导出
     * @param response
     * @throws IOException
     */
    @Override
    public void downloadExcel(HttpServletResponse response) throws IOException {
        List<VisitorVO> visitorVOS= visitorsMapper.selectAllVisitors();
        //设置文件下载
        //设置响应头，告诉浏览器要以文件的形式保存
//        response.setHeader();
        response.setHeader("content-disposition","attachment;filename="+System.currentTimeMillis()+".xlsx");
        //生成Excel并写入
        EasyExcel.write(response.getOutputStream(), VisitorVO.class).sheet(System.currentTimeMillis()+"").doWrite(visitorVOS);

    }

    @Override
    public List<VisitorVO> selectAllVisitors() {
        return visitorsMapper.selectAllVisitors();
    }

}
